package MypageController.MyWrite;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.PagingManager;

import MypageController.MyComment.MyCommentBean;
import MypageController.MyKategore.MyKategoreBean;
import MypageController.MyMain.MyMainBean;
import MypageController.MyMain.MyMainDAO;

/**
 * Servlet implementation class MyWriteServ
 */
public class MyWriteServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyWriteServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=UTF-8");
 		RequestDispatcher view = null;
		MyMainDAO mymaindao = new MyMainDAO();
		MyMainBean mymainbean = new MyMainBean();
		MyWriteDAO mywritedao = new MyWriteDAO();
		
		MyWriteBean[] mywritelist=null;
		MyCommentBean[] mycommentbean = null;
		MyCommentBean[] entriesmycommentbean = null;
		MyKategoreBean[] kategorilist = null;
		MyWriteBean mywritebean = new MyWriteBean();
		
		int mypageid = Integer.parseInt(request.getParameter("mypageid"));
 		String action=request.getParameter("action");
 		String id=request.getParameter("id");
 		String url=null;
 		
 		if(action.equals("writeview")){
 				
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mycommentbean = mymaindao.latestComment(mypageid);
 			mymainbean = mymaindao.myPageDataGet(mypageid);
 			int level = mymaindao.level(id);
 			
 			String pagename = "/Mypage/Mp_Write";
 			request.setAttribute("page", pagename);
 			request.setAttribute("id", id);
 			request.setAttribute("mypagedata", mymainbean);
 			request.setAttribute("level", level);
 			request.setAttribute("mypage", "mypage");
			request.setAttribute("mypageid", mypageid);
			request.setAttribute("kategorilist", kategorilist);
			request.setAttribute("kategdate", kategdate);
			request.setAttribute("latestcomment", mycommentbean);
			
			if(level == 1){
				url="/Main/OldMain/Om_Main.jsp";
			}else{
				url="/Main/YoungMain/Ym_Main.jsp";
			}
	
			view=request.getRequestDispatcher(url);
			view.forward(request,response); 
 			
 		}else if(action.equals("write")){
 			
 			String selectkate = request.getParameter("kate");
 			String content = request.getParameter("content");
 			String title = request.getParameter("title");
 			title = title.replaceAll(">", "&gt;");
 			title = title.replaceAll("<", "&lt;");

System.out.println("블로그 번호 : " + mypageid + " || 글슨이 : " + id + " || 선택 카테고리 : " + selectkate + " || 글 내용 : " + content );

 			mywritebean.setBlogs_Id(mypageid);
 			mywritebean.setEnt_Title(title);
 			mywritebean.setEnt_Contents(content);
 			mywritebean.setEnt_Kategorie(selectkate);
 			mywritebean.setEnt_Nick(id);
 			
 			mywritedao.entrieSet(mywritebean);
 			
 			int currentPage = 1;
 			
 			HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			
			param.put("pageSize", 7);
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			int count = mymaindao.entriesCount(mypageid);
			param.put("totalCount", count);
			
			PagingManager paging = new common.PagingManager();
			paging.setPageNo(param);
 			
			Integer[] entriesidlist = mymaindao.entriesIdGet(mypageid);
			
			for(int i = 0; i<entriesidlist.length;i++){		
				mymaindao.commentCountSet(entriesidlist[i]);
				mymaindao.entriesNumberSet(entriesidlist[i], i);
			}
			
 			mywritelist = mymaindao.entriesGet(mypageid, currentPage, 7);
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mycommentbean = mymaindao.latestComment(mypageid);
 			mymainbean = mymaindao.myPageDataGet(mypageid);
 			int level = mymaindao.level(id);
 			
			String pagename = "/Mypage/Mp_Bord";
			request.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page", pagename);
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("id", id);
			request.setAttribute("mypagedata", mymainbean);
			request.setAttribute("mypage", "mypage");
			request.setAttribute("level", level);
			request.setAttribute("mypageid", mypageid);
			request.setAttribute("entries", mywritelist);
			request.setAttribute("kategorilist", kategorilist);
			request.setAttribute("kategdate", kategdate);
			request.setAttribute("latestcomment", mycommentbean);
			
			if(level == 1){
				url="/Main/OldMain/Om_Main.jsp";
			}else{
				url="/Main/YoungMain/Ym_Main.jsp";
			}
	
			view=request.getRequestDispatcher(url);
			view.forward(request,response);
 				
 		}else if(action.equals("modify")){
 			
 			int entriid = Integer.parseInt(request.getParameter("entriid"));
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mycommentbean = mymaindao.latestComment(mypageid);
 			mywritebean = mywritedao.entrieSee(entriid);
 			mymainbean = mymaindao.myPageDataGet(mypageid);
 			int level = mymaindao.level(id);

			String pagename = "/Mypage/Mp_Modify";
			request.setAttribute("page", pagename);
 			request.setAttribute("id", id);
 			request.setAttribute("mypagedata", mymainbean);
 			request.setAttribute("level", level);
 			request.setAttribute("mypage", "mypage");
 			request.setAttribute("mypageid", mypageid);
 			request.setAttribute("mypagecontent", mywritebean);
 			request.setAttribute("kategorilist", kategorilist);
			request.setAttribute("kategdate", kategdate);
			request.setAttribute("latestcomment", mycommentbean);
			
			if(level == 1){
				url="/Main/OldMain/Om_Main.jsp";
			}else{
				url="/Main/YoungMain/Ym_Main.jsp";
			}
	
			view=request.getRequestDispatcher(url);
			view.forward(request,response);
			
 		}else if(action.equals("writemodify")){
 			
 			String selectkate = request.getParameter("kate");
 			String content = request.getParameter("content");
 			String title = request.getParameter("title");
 			int entriid = Integer.parseInt(request.getParameter("entriid"));
 			int number = Integer.parseInt(request.getParameter("number"));
 			
System.out.println("블로그 번호 : " + mypageid + " || 글슨이 : " + id + " || 선택 카테고리 : " + selectkate + " || 글 내용 : " + content );

			mywritebean.setID(entriid);
 			mywritebean.setBlogs_Id(mypageid);
 			mywritebean.setEnt_Title(title);
 			mywritebean.setEnt_Contents(content);
 			mywritebean.setEnt_Kategorie(selectkate);
 			mywritebean.setEnt_Nick(id);
 			
 			mywritedao.entrieUpdate(mywritebean);
 
 			mywritelist = mymaindao.latestEntriesGet(mypageid, number);
 			int entriesnumber = mywritebean.getID();
 			entriesmycommentbean = mymaindao.commentGet(mypageid, entriesnumber);
 			Integer[] entriesid = mymaindao.entriesIdGet(mypageid);
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mycommentbean = mymaindao.latestComment(mypageid);
			for(int i=0;i<mycommentbean.length;i++){	
				int entriesnum = mymaindao.entriesNumberGet(mycommentbean[i].getEntries_Id());
				mycommentbean[i].setEntries_Number(entriesnum);
			}	
			mymainbean = mymaindao.myPageDataGet(mypageid);
 			int level = mymaindao.level(id);

			String pagename = "/Mypage/Mp_Read";
			request.setAttribute("page", pagename);
			request.setAttribute("number", number);
			request.setAttribute("numbermax", entriesid.length - 1);
			request.setAttribute("id", id);
			request.setAttribute("mypagedata", mymainbean);
			request.setAttribute("mypage", "mypage");
			request.setAttribute("level", level);
			request.setAttribute("mypageid", mypageid);
			request.setAttribute("entries", mywritelist);
			request.setAttribute("kategorilist", kategorilist);
			request.setAttribute("kategdate", kategdate);
			request.setAttribute("latestcomment", mycommentbean);
			request.setAttribute("commentlist", entriesmycommentbean);
			
			if(level == 1){
				url="/Main/OldMain/Om_Main.jsp";
			}else{
				url="/Main/YoungMain/Ym_Main.jsp";
			}
	
			view=request.getRequestDispatcher(url);
			view.forward(request,response); 
			
 		}else if(action.equals("entriDelet")){
 			
 			int entriesid = Integer.parseInt(request.getParameter("entriid"));
 			int currentPage = Integer.parseInt(request.getParameter("currentPage"));
 			mywritedao.entrieDelet(mypageid, entriesid);
		
 			int count = mymaindao.entriesCount(mypageid);

 			if((currentPage * 7)-7 >= count){	
 				if((currentPage * 7)-7 == 0){
 					currentPage = 1;
 				}else{
 					currentPage = currentPage - 1;
 				}
 			}	

 			HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			
			param.put("pageSize", 7);
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			param.put("totalCount", count);
			
			PagingManager paging = new common.PagingManager();
			paging.setPageNo(param);
 			
			Integer[] entriesidlist = mymaindao.entriesIdGet(mypageid);
			
			for(int i = 0; i<entriesidlist.length;i++){		
				mymaindao.commentCountSet(entriesidlist[i]);
				mymaindao.entriesNumberSet(entriesidlist[i], i);
			}
			
 			mywritelist = mymaindao.entriesGet(mypageid, currentPage, 7);
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mycommentbean = mymaindao.latestComment(mypageid);
 			mymainbean = mymaindao.myPageDataGet(mypageid);
 			int level = mymaindao.level(id);
 			
			String pagename = "/Mypage/Mp_Bord";
			request.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page", pagename);
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("id", id);
			request.setAttribute("mypagedata", mymainbean);
			request.setAttribute("mypage", "mypage");
			request.setAttribute("level", level);
			request.setAttribute("mypageid", mypageid);
			request.setAttribute("entries", mywritelist);
			request.setAttribute("kategorilist", kategorilist);
			request.setAttribute("kategdate", kategdate);
			request.setAttribute("latestcomment", mycommentbean);
			
			if(level == 1){
				url="/Main/OldMain/Om_Main.jsp";
			}else{
				url="/Main/YoungMain/Ym_Main.jsp";
			}
	
			view=request.getRequestDispatcher(url);
			view.forward(request,response);
 			
 		}
	}
}
